package com.jy.admin.jobs;

import com.fqgj.base.services.oss.OSSService;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.jy.admin.config.ApolloConfigUtil;
import com.jy.admin.db.dao.CeAgentAccountDao;
import com.jy.admin.db.dao.CeMerchantDao;
import com.jy.admin.db.dao.CePushLogDao;
import com.jy.admin.db.entity.CeAgentAccountEntity;
import com.jy.admin.db.entity.CeMerchantEntity;
import com.jy.admin.services.BaseCrawlerService;
import com.robert.vesta.util.IpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2022/1/3
 * Time: 6:18 下午
 */
@Service
public class BaseJobHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseJobHandler.class);
    private static ExecutorService pool = Executors.newFixedThreadPool(5);
    public static Map<String, BaseCrawlerService> merchantAndServiceMap = new HashMap<>();

    @Autowired
    private CeMerchantDao merchantDao;

    @Autowired
    private CeAgentAccountDao accountDao;

    @Autowired
    private ApolloConfigUtil configUtil;

    @Autowired
    public OSSService ossService;


    @Autowired
    public CePushLogDao pushLogDao;

    @Scheduled(fixedDelay = 1000L * 300)
    public void refreshDataRaiseCash() {
        LOGGER.info("== BaseJobHandler 开始 raisecash ==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity raisecash = merchantDao.queryMerchantByCode("raisecash");
        if (raisecash != null) {
            allActiveMerchant.add(raisecash);
        }

        run(allActiveMerchant);
    }


    @Scheduled(fixedDelay = 1000L * 300)
    public void dos() {
        LOGGER.info("== BaseJobHandler 开始 dos ==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity dos = merchantDao.queryMerchantByCode("dos");
        if (dos != null) {
            allActiveMerchant.add(dos);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void rupeebag() {
        LOGGER.info("== BaseJobHandler 开始 dos ==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity rupeebag = merchantDao.queryMerchantByCode("rupeebag");
        if (rupeebag != null) {
            allActiveMerchant.add(rupeebag);
        }

        run(allActiveMerchant);
    }


    @Scheduled(fixedDelay = 1000L * 300)
    public void pp() {
        LOGGER.info("== BaseJobHandler 开始 papamoney==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity papamoney = merchantDao.queryMerchantByCode("papamoney");
        if (papamoney != null) {
            allActiveMerchant.add(papamoney);
        }
        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void pp2() {
        LOGGER.info("== BaseJobHandler 开始 papamoney==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity papamoney2 = merchantDao.queryMerchantByCode("papamoney2");
        if (papamoney2 != null) {
            allActiveMerchant.add(papamoney2);
        }
        run(allActiveMerchant);
    }


    @Scheduled(fixedDelay = 1000L * 300)
    public void chenxing() {
        LOGGER.info("== BaseJobHandler 开始 chenxing==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity chenxing = merchantDao.queryMerchantByCode("chenxing");
        if (chenxing != null) {
            allActiveMerchant.add(chenxing);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void chenxingnew() {
        LOGGER.info("== BaseJobHandler 开始 chenxingnew==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity chenxingnew = merchantDao.queryMerchantByCode("chenxingnew");
        if (chenxingnew != null) {
            allActiveMerchant.add(chenxingnew);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void okrupee() {
        LOGGER.info("== BaseJobHandler 开始 okrupee==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity okrupee = merchantDao.queryMerchantByCode("okrupee");
        if (okrupee != null) {
            allActiveMerchant.add(okrupee);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void cashmart() {
        LOGGER.info("== BaseJobHandler 开始 cashmart==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity cashmart = merchantDao.queryMerchantByCode("cashmart");
        if (cashmart != null) {
            allActiveMerchant.add(cashmart);
        }

        run(allActiveMerchant);
    }


    @Scheduled(fixedDelay = 1000L * 1800)
    public void cashplanner() {
        LOGGER.info("== BaseJobHandler 开始 sareecash==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity cashplanner = merchantDao.queryMerchantByCode("cashplanner");
        if (cashplanner != null) {
            allActiveMerchant.add(cashplanner);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 600)
    public void littleorange() {
        LOGGER.info("== BaseJobHandler 开始 littleorange==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity littleorange = merchantDao.queryMerchantByCode("littleorange");
        if (littleorange != null) {
            allActiveMerchant.add(littleorange);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 1800)
    public void papamoney3() {
        LOGGER.info("== BaseJobHandler 开始 cashg==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity papamoney3 = merchantDao.queryMerchantByCode("papamoney3");
        if (papamoney3 != null) {
            allActiveMerchant.add(papamoney3);
        }

        runM(allActiveMerchant);
    }


    @Scheduled(fixedDelay = 1000L * 200)
    public void cashbro() {
        LOGGER.info("== BaseJobHandler 开始 cashbro==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity cashbro = merchantDao.queryMerchantByCode("cashbro");
        if (cashbro != null) {
            allActiveMerchant.add(cashbro);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 200)
    public void naanloan() {
        LOGGER.info("== BaseJobHandler 开始 naanloan==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity naanloan = merchantDao.queryMerchantByCode("naanloan");
        if (naanloan != null) {
            allActiveMerchant.add(naanloan);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 200)
    public void quickcash() {
        LOGGER.info("== BaseJobHandler 开始 quickcash==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity quickcash = merchantDao.queryMerchantByCode("quickcash");
        if (quickcash != null) {
            allActiveMerchant.add(quickcash);
        }

        run(allActiveMerchant);
    }
    @Scheduled(fixedDelay = 1000L * 200)
    public void rupeedidi() {
        LOGGER.info("== BaseJobHandler 开始 rupeedidi==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity rupeedidi = merchantDao.queryMerchantByCode("rupeedidi");
        if (rupeedidi != null) {
            allActiveMerchant.add(rupeedidi);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 200)
    public void pravin() {
        LOGGER.info("== BaseJobHandler 开始 pravin==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity pravin = merchantDao.queryMerchantByCode("pravin");
        if (pravin != null) {
            allActiveMerchant.add(pravin);
        }

        run(allActiveMerchant);
    }
    @Scheduled(fixedDelay = 1000L * 200)
    public void swiftcash() {
        LOGGER.info("== BaseJobHandler 开始 swiftcash==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity swiftcash = merchantDao.queryMerchantByCode("swiftcash");
        if (swiftcash != null) {
            allActiveMerchant.add(swiftcash);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 200)
    public void korma() {
        LOGGER.info("== BaseJobHandler 开始 korma==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity korma = merchantDao.queryMerchantByCode("korma");
        if (korma != null) {
            allActiveMerchant.add(korma);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 200)
    public void prettyloan() {
        LOGGER.info("== BaseJobHandler 开始 prettyloan==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity prettyloan = merchantDao.queryMerchantByCode("prettyloan");
        if (prettyloan != null) {
            allActiveMerchant.add(prettyloan);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void cashbroold() {
        LOGGER.info("== BaseJobHandler 开始 cashbroold==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity cashbroold = merchantDao.queryMerchantByCode("cashbroold");
        if (cashbroold != null) {
            allActiveMerchant.add(cashbroold);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void rupeeyaaron() {
        LOGGER.info("== BaseJobHandler 开始  rupeeyaaron==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity cashbroold = merchantDao.queryMerchantByCode("rupeeyaaron");
        if (cashbroold != null) {
            allActiveMerchant.add(cashbroold);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void walnutsloan() {
        LOGGER.info("== BaseJobHandler 开始  walnutsloan==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity walnutsloan = merchantDao.queryMerchantByCode("walnutsloan");
        if (walnutsloan != null) {
            allActiveMerchant.add(walnutsloan);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void idli() {
        LOGGER.info("== BaseJobHandler 开始  idli==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity idli = merchantDao.queryMerchantByCode("idli");
        if (idli != null) {
            allActiveMerchant.add(idli);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void poppins() {
        LOGGER.info("== BaseJobHandler 开始  poppins==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity cashbroold = merchantDao.queryMerchantByCode("poppins");
        if (cashbroold != null) {
            allActiveMerchant.add(cashbroold);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void moneyloan() {
        LOGGER.info("== BaseJobHandler 开始  moneyloan==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity moneyloan = merchantDao.queryMerchantByCode("moneyloan");
        if (moneyloan != null) {
            allActiveMerchant.add(moneyloan);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void cashloan() {
        LOGGER.info("== BaseJobHandler 开始  cashloan==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity cashloan = merchantDao.queryMerchantByCode("cashloan");
        if (cashloan != null) {
            allActiveMerchant.add(cashloan);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void everymoney() {
        LOGGER.info("== BaseJobHandler 开始  everymoney==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity everymoney = merchantDao.queryMerchantByCode("everymoney");
        if (everymoney != null) {
            allActiveMerchant.add(everymoney);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void tenzin() {
        LOGGER.info("== BaseJobHandler 开始  tenzin==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity tenzin = merchantDao.queryMerchantByCode("tenzin");
        if (tenzin != null) {
            allActiveMerchant.add(tenzin);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void drisana() {
        LOGGER.info("== BaseJobHandler 开始  drisana==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity drisana = merchantDao.queryMerchantByCode("drisana");
        if (drisana != null) {
            allActiveMerchant.add(drisana);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void selina() {
        LOGGER.info("== BaseJobHandler 开始  everymoney==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity selina = merchantDao.queryMerchantByCode("selina");
        if (selina != null) {
            allActiveMerchant.add(selina);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void pagli() {
        LOGGER.info("== BaseJobHandler 开始  pagli==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity pagli = merchantDao.queryMerchantByCode("pagli");
        if (pagli != null) {
            allActiveMerchant.add(pagli);
        }

        run(allActiveMerchant);
    }

    @Scheduled(fixedDelay = 1000L * 300)
    public void asher() {
        LOGGER.info("== BaseJobHandler 开始  asher==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity asher = merchantDao.queryMerchantByCode("asher");
        if (asher != null) {
            allActiveMerchant.add(asher);
        }

        run(allActiveMerchant);
    }


    @Scheduled(fixedDelay = 1000L * 200)
    public void irupee() {
        LOGGER.info("== BaseJobHandler 开始 irupee==");
        List<CeMerchantEntity> allActiveMerchant = new ArrayList<>();
        CeMerchantEntity irupee = merchantDao.queryMerchantByCode("irupee");
        if (irupee != null) {
            allActiveMerchant.add(irupee);
        }

        run(allActiveMerchant);
    }


//    @Scheduled(fixedDelay = 1000L * 60)
//    public void pushDataToCollection() {
//        String hostIp = IpUtils.getHostIp();
//        if (!hostIp.equals(configUtil.getJobRunIp())) {
//            LOGGER.info("==PayCallBackToBizJob==, 当前机器ip不是job执行机器,当前iP:{},目标IP:{}", hostIp, configUtil.getJobRunIp());
//            return;
//        }
//
//        LOGGER.info("== pushDataToCollection 开始 pushDataToCollection==");
//        List<CeMerchantEntity> allActiveMerchant = merchantDao.getAllActiveMerchant();
//
//        allActiveMerchant.forEach(merchant -> {
//            try {
//                LOGGER.info("== pushDataToCollection 开始处理商户 ==,merchantCode:{}", merchant.getMerchantCode());
//
//                if (!merchantAndServiceMap.containsKey(merchant.getMerchantCode())) {
//                    LOGGER.error("== pushDataToCollection ==,找不到商户的处理逻辑，merchant:{}", merchant.getMerchantCode());
//                    return;
//                }
//
//                List<CeAgentAccountEntity> accounts = accountDao.getAccountsMerchantCode(merchant.getMerchantCode());
//                if (CollectionUtils.isEmpty(accounts)) {
//                    LOGGER.info("== pushDataToCollection 商户下面没有账号 ==,merchantCode:{}", merchant.getMerchantCode());
//                    return;
//                }
//
//                accounts.forEach(account -> {
//                    try {
//
//                        if (merchant.getStatus() == 2) {
//                            LOGGER.info("== BaseJobHandler 开始处理商户,商户当前处于不可更新状态，status=2 ==,merchantCode:{}", merchant.getMerchantCode());
//                            return;
//                        }
//
//                        LOGGER.info("== pushDataToCollection 开始处理账号 ==,account:{}", account.getAccount());
//                        LOGGER.info("== pushDataToCollection 开始推送数据到催收 ==,account:{}", account.getAccount());
//
//                        merchantAndServiceMap.get(account.getMerchantCode()).pushDataToCollection(account);
//
//                        LOGGER.info("== pushDataToCollection 开始处理 当前账号处理完成==,，account:{}", account.getAccount());
//                    } catch (Exception e) {
//                        LOGGER.error("== pushDataToCollection 开始处理 异常==,，account:{}", account.getAccount(), e);
//                    }
//                });
//
//                LOGGER.info("== pushDataToCollection 开始处理 当前商户处理完成==,，merchant:{}", merchant.getMerchantCode());
//
//            } catch (Exception e) {
//                LOGGER.error("pushDataToCollection 异常", e);
//            }
//        });
//    }


    @Scheduled(fixedDelay = 1000L * 60)
    public void pushPaidUpdate() {

        if (!configUtil.pushPaidUpdateSwitch()) {
            LOGGER.info("== pushPaidUpdate switch not open==");
            return;
        }

        String hostIp = IpUtils.getHostIp();
        if (!hostIp.equals(configUtil.getJobRunIp())) {
            LOGGER.info("==pushPaidUpdate==, 当前机器ip不是job执行机器,当前iP:{},目标IP:{}", hostIp, configUtil.getJobRunIp());
            return;
        }

        LOGGER.info("== pushPaidUpdate 开始==");
        List<CeMerchantEntity> allActiveMerchant = merchantDao.getAllActiveMerchant();

        allActiveMerchant.forEach(merchant -> {
            LOGGER.info("== pushPaidUpdate 开始处理商户 ==,merchantCode:{}", merchant.getMerchantCode());

            if (!merchantAndServiceMap.containsKey(merchant.getMerchantCode())) {
                LOGGER.error("== pushPaidUpdate ==,找不到商户的处理逻辑，merchant:{}", merchant.getMerchantCode());
                return;
            }

            List<CeAgentAccountEntity> accounts = accountDao.queryAll();
            if (CollectionUtils.isEmpty(accounts)) {
                LOGGER.info("== pushPaidUpdate 商户下面没有账号 ==,merchantCode:{}", merchant.getMerchantCode());
                return;
            }

            accounts.forEach(account -> {
                try {
                    if (merchant.getStatus() == 2) {
                        LOGGER.info("== pushPaidUpdate 开始处理商户,商户当前处于不可更新状态，status=2 ==,merchantCode:{}", merchant.getMerchantCode());
                        return;
                    }
                    LOGGER.info("== pushPaidUpdate 开始处理账号 ==,account:{}", account.getAccount());
                    merchantAndServiceMap.get(account.getMerchantCode()).updatePaidOrderToCollection(account);
                    LOGGER.info("== pushPaidUpdate 开始处理 当前账号处理完成==,，account:{}", account.getAccount());
                } catch (Exception e) {
                    LOGGER.error("== pushPaidUpdate 开始处理 异常==,，account:{}", account.getAccount(), e);
                }
            });

            LOGGER.info("== pushPaidUpdate 开始处理 当前商户处理完成==,，merchant:{}", merchant.getMerchantCode());
        });
    }

    public void run(List<CeMerchantEntity> allActiveMerchant) {
        String hostIp = IpUtils.getHostIp();
        LOGGER.info("==PayCallBackToBizJob==,ip:{}", hostIp);

        if (!hostIp.equals(configUtil.getJobRunIp())) {
            LOGGER.info("==PayCallBackToBizJob==, 当前机器ip不是job执行机器,当前iP:{},目标IP:{}", hostIp, configUtil.getJobRunIp());
            return;
        }

        LOGGER.info("== BaseJobHandler 开始 ==");
        if (!configUtil.getBaseJobSwitch()) {
            LOGGER.info("== refreshData switch not open ==");
            return;
        }
        if (CollectionUtils.isEmpty(allActiveMerchant)) {
            LOGGER.info("== BaseJobHandler 没有活跃的商户 ==");
            return;
        }

        if (DateUtil.getHour() < configUtil.getCurrentWorkTime()) {
            LOGGER.info("== BaseJobHandler 当前时间小于7点不能开始工作 ==");
            return;
        }


        LOGGER.info("== BaseJobHandler ==,size:{}", allActiveMerchant.size());

        allActiveMerchant.forEach(merchant -> {
            LOGGER.info("== BaseJobHandler 开始处理商户 ==,merchantCode:{}", merchant.getMerchantCode());
            if (merchant.getStatus() == 2) {
                LOGGER.info("== BaseJobHandler 开始处理商户,商户当前处于不可更新状态，status=2 ==,merchantCode:{}", merchant.getMerchantCode());
                return;
            }

            if (!merchantAndServiceMap.containsKey(merchant.getMerchantCode())) {
                LOGGER.error("== BaseJobHandler ==,找不到商户的处理逻辑，merchant:{}", merchant.getMerchantCode());
                return;
            }

            List<CeAgentAccountEntity> accounts = accountDao.getAccountsMerchantCode(merchant.getMerchantCode());
            if (CollectionUtils.isEmpty(accounts)) {
                LOGGER.info("== BaseJobHandler 商户下面没有账号 ==,merchantCode:{}", merchant.getMerchantCode());
                return;
            }

            accounts.forEach(account -> {
                try {
                    if (merchant.getStatus() == 2) {
                        LOGGER.info("== BaseJobHandler 开始处理商户,商户当前处于不可更新状态，status=2 ==,merchantCode:{}", merchant.getMerchantCode());
                        return;
                    }
                    if (account.getLoginStatus() != 1) {
                        LOGGER.info("== BaseJobHandler 开始处理商户,账号登陆状态失败==,merchantCode:{}", merchant.getMerchantCode());
                        return;
                    }
                    LOGGER.info("== BaseJobHandler 开始处理账号 ==,account:{}", account.getAccount());
                    LOGGER.info("== BaseJobHandler 开始处理 刷新订单==,，account:{}", account.getAccount());
                    merchantAndServiceMap.get(account.getMerchantCode()).refresh(account);

                    LOGGER.info("== BaseJobHandler 开始处理 抓取先关数据==,，account:{}", account.getAccount());
                    merchantAndServiceMap.get(account.getMerchantCode()).compostCollectionData(account);

                    LOGGER.info("== BaseJobHandler 开始处理 检查还款订单==,，account:{}", account.getAccount());
                    merchantAndServiceMap.get(account.getMerchantCode()).checkOrdersPaidStatus(account);
                    LOGGER.info("== BaseJobHandler 开始处理 当前账号处理完成==,，account:{}", account.getAccount());
                } catch (Exception e) {
                    LOGGER.error("== BaseJobHandler 开始处理 异常==,，account:{}", account.getAccount(), e);
                }
            });

            LOGGER.info("== BaseJobHandler 开始处理 当前商户处理完成==,，merchant:{}", merchant.getMerchantCode());
        });

        LOGGER.info("== BaseJobHandler 开始处理 所有商户处理完成==");
    }

    public void runM(List<CeMerchantEntity> allActiveMerchant) {
        String hostIp = IpUtils.getHostIp();
        LOGGER.info("==PayCallBackToBizJob==,ip:{}", hostIp);

        if (hostIp.equals(configUtil.getJobRunIp())) {
            LOGGER.info("==PayCallBackToBizJob==, 当前机器ip不是job执行机器,当前iP:{},目标IP:{}", hostIp, configUtil.getJobRunIp());
            return;
        }

        LOGGER.info("== BaseJobHandler 开始 ==");
        if (!configUtil.getBaseJobSwitch()) {
            LOGGER.info("== refreshData switch not open ==");
            return;
        }
        if (CollectionUtils.isEmpty(allActiveMerchant)) {
            LOGGER.info("== BaseJobHandler 没有活跃的商户 ==");
            return;
        }

        if (DateUtil.getHour() < configUtil.getCurrentWorkTime()) {
            LOGGER.info("== BaseJobHandler 当前时间小于7点不能开始工作 ==");
            return;
        }


        LOGGER.info("== BaseJobHandler ==,size:{}", allActiveMerchant.size());

        allActiveMerchant.forEach(merchant -> {
            LOGGER.info("== BaseJobHandler 开始处理商户 ==,merchantCode:{}", merchant.getMerchantCode());
            if (merchant.getStatus() == 2) {
                LOGGER.info("== BaseJobHandler 开始处理商户,商户当前处于不可更新状态，status=2 ==,merchantCode:{}", merchant.getMerchantCode());
                return;
            }

            if (!merchantAndServiceMap.containsKey(merchant.getMerchantCode())) {
                LOGGER.error("== BaseJobHandler ==,找不到商户的处理逻辑，merchant:{}", merchant.getMerchantCode());
                return;
            }

            List<CeAgentAccountEntity> accounts = accountDao.getAccountsMerchantCode(merchant.getMerchantCode());
            if (CollectionUtils.isEmpty(accounts)) {
                LOGGER.info("== BaseJobHandler 商户下面没有账号 ==,merchantCode:{}", merchant.getMerchantCode());
                return;
            }

            accounts.forEach(account -> {
                try {
                    if (merchant.getStatus() == 2) {
                        LOGGER.info("== BaseJobHandler 开始处理商户,商户当前处于不可更新状态，status=2 ==,merchantCode:{}", merchant.getMerchantCode());
                        return;
                    }
                    if (account.getLoginStatus() != 1) {
                        LOGGER.info("== BaseJobHandler 开始处理商户,账号登陆状态失败==,merchantCode:{}", merchant.getMerchantCode());
                        return;
                    }
                    LOGGER.info("== BaseJobHandler 开始处理账号 ==,account:{}", account.getAccount());
                    LOGGER.info("== BaseJobHandler 开始处理 刷新订单==,，account:{}", account.getAccount());
                    merchantAndServiceMap.get(account.getMerchantCode()).refresh(account);

                    LOGGER.info("== BaseJobHandler 开始处理 抓取先关数据==,，account:{}", account.getAccount());
                    merchantAndServiceMap.get(account.getMerchantCode()).compostCollectionData(account);

                    LOGGER.info("== BaseJobHandler 开始处理 检查还款订单==,，account:{}", account.getAccount());
                    merchantAndServiceMap.get(account.getMerchantCode()).checkOrdersPaidStatus(account);
                    LOGGER.info("== BaseJobHandler 开始处理 当前账号处理完成==,，account:{}", account.getAccount());
                } catch (Exception e) {
                    LOGGER.error("== BaseJobHandler 开始处理 异常==,，account:{}", account.getAccount(), e);
                }
            });

            LOGGER.info("== BaseJobHandler 开始处理 当前商户处理完成==,，merchant:{}", merchant.getMerchantCode());
        });

        LOGGER.info("== BaseJobHandler 开始处理 所有商户处理完成==");
    }

    @Scheduled(fixedDelay = 1000L * 90)
    public void checkLoginJob() {
        String hostIp = IpUtils.getHostIp();
        LOGGER.info("==checkLoginJob==,ip:{}", hostIp);

        if (!hostIp.equals(configUtil.getJobRunIp())) {
            LOGGER.info("==checkLoginJob==, 当前机器ip不是job执行机器,当前iP:{},目标IP:{}", hostIp, configUtil.getJobRunIp());
            return;
        }

        if (!configUtil.checkLoginSwitch()) {
            LOGGER.info("== checkLoginJob switch not open 开始 ==");
            return;
        }

        if (DateUtil.getHour() < configUtil.getCurrentWorkTime()) {
            LOGGER.info("== BaseJobHandler 当前时间小于7点不能开始工作 ==");
            return;
        }

        LOGGER.info("== checkLoginJob 开始 ==");

        List<CeMerchantEntity> allActiveMerchant = merchantDao.getAllActiveMerchant();
        if (CollectionUtils.isEmpty(allActiveMerchant)) {
            LOGGER.info("== checkLoginJob 没有活跃的商户 ==");
            return;
        }

        LOGGER.info("== checkLoginJob ==,size:{}", allActiveMerchant.size());


        allActiveMerchant.forEach(merchant -> {
            LOGGER.info("== checkLoginJob 开始处理商户 ==,merchantCode:{}", merchant.getMerchantCode());
            if (!merchantAndServiceMap.containsKey(merchant.getMerchantCode())) {
                LOGGER.error("== checkLoginJob ==,找不到商户的处理逻辑，merchant:{}", merchant.getMerchantCode());
                return;
            }

            List<CeAgentAccountEntity> accounts = accountDao.getAccountsMerchantCode(merchant.getMerchantCode());
            if (CollectionUtils.isEmpty(accounts)) {
                LOGGER.info("== checkLoginJob 商户下面没有账号 ==,merchantCode:{}", merchant.getMerchantCode());
                return;
            }

            accounts.forEach(account -> {
                try {
                    LOGGER.info("== checkLoginJob 开始处理账号 ==,account:{}", account.getAccount());
                    if (!merchantAndServiceMap.get(account.getMerchantCode()).hasLogin(account)) {
                        LOGGER.error("== checkLoginJob 账号未登录 ==,account:{}", account.getAccount());
                    }
                } catch (Exception e) {
                    LOGGER.error("== checkLoginJob 开始处理 异常==,，account:{}", account.getAccount(), e);
                }
            });

            LOGGER.info("== checkLoginJob 开始处理 当前商户处理完成==,，merchant:{}", merchant.getMerchantCode());
        });

        LOGGER.info("== checkLoginJob 开始处理 所有商户处理完成==");
    }

    @Scheduled(fixedDelay = 1000L * 1600)
    public void checkLoginJobNight() {
        String hostIp = IpUtils.getHostIp();
        LOGGER.info("==checkLoginJob==,ip:{}", hostIp);

        if (!hostIp.equals(configUtil.getJobRunIp())) {
            LOGGER.info("==checkLoginJob==, 当前机器ip不是job执行机器,当前iP:{},目标IP:{}", hostIp, configUtil.getJobRunIp());
            return;
        }

        if (!configUtil.checkLoginSwitch()) {
            LOGGER.info("== checkLoginJob switch not open 开始 ==");
            return;
        }

        if (DateUtil.getHour() > configUtil.getCurrentWorkTime()) {
            LOGGER.info("== BaseJobHandler 当前时间大于设置时间不能开始工作 ==");
            return;
        }

        LOGGER.info("== checkLoginJob 开始 ==");

        List<CeMerchantEntity> allActiveMerchant = merchantDao.getAllActiveMerchant();
        if (CollectionUtils.isEmpty(allActiveMerchant)) {
            LOGGER.info("== checkLoginJob 没有活跃的商户 ==");
            return;
        }

        LOGGER.info("== checkLoginJob ==,size:{}", allActiveMerchant.size());


        allActiveMerchant.forEach(merchant -> {
            LOGGER.info("== checkLoginJob 开始处理商户 ==,merchantCode:{}", merchant.getMerchantCode());
            if (!merchantAndServiceMap.containsKey(merchant.getMerchantCode())) {
                LOGGER.error("== checkLoginJob ==,找不到商户的处理逻辑，merchant:{}", merchant.getMerchantCode());
                return;
            }

            List<CeAgentAccountEntity> accounts = accountDao.getAccountsMerchantCode(merchant.getMerchantCode());
            if (CollectionUtils.isEmpty(accounts)) {
                LOGGER.info("== checkLoginJob 商户下面没有账号 ==,merchantCode:{}", merchant.getMerchantCode());
                return;
            }

            accounts.forEach(account -> {
                try {
                    LOGGER.info("== checkLoginJob 开始处理账号 ==,account:{}", account.getAccount());
                    if (!merchantAndServiceMap.get(account.getMerchantCode()).hasLogin(account)) {
                        LOGGER.error("== checkLoginJob 账号未登录 ==,account:{}", account.getAccount());
                    }
                } catch (Exception e) {
                    LOGGER.error("== checkLoginJob 开始处理 异常==,，account:{}", account.getAccount(), e);
                }
            });

            LOGGER.info("== checkLoginJob 开始处理 当前商户处理完成==,，merchant:{}", merchant.getMerchantCode());
        });

        LOGGER.info("== checkLoginJob 开始处理 所有商户处理完成==");

    }

    @Scheduled(fixedDelay = 1000L * 600)
    public void closeJob() {
        if (DateUtil.getHour() == 21 && DateUtil.getMinute() >= configUtil.getCloseWorkTime()) {
            LOGGER.info("== BaseJobHandler 当前时间大于晚上10点50后关闭除coinrupees外的所有商户 ==");
            List<CeMerchantEntity> allActiveMerchant = merchantDao.getAllActiveMerchant();
            allActiveMerchant.forEach(ceMerchantEntity -> {
                ceMerchantEntity.setStatus(2);
                merchantDao.updateByPrimaryKey(ceMerchantEntity);
            });
        }
    }

    @Scheduled(fixedDelay = 1000L * 600)
    public void startJob() {
        if (DateUtil.getHour() == 7) {
            LOGGER.info("== BaseJobHandler 早上9点多开始拉取数据 ==");
            List<CeMerchantEntity> allActiveMerchant = merchantDao.getAllActiveMerchant();
            allActiveMerchant.forEach(ceMerchantEntity -> {
                if (ceMerchantEntity.getAuto() == 1) {
                    ceMerchantEntity.setStatus(1);
                    merchantDao.updateByPrimaryKey(ceMerchantEntity);
                }
            });
        }
    }

}
